App.module.Language.Add = Ext.extend(Ext.util.Observable, {
	init: function(tree){
		this.tree = tree;
		tree.on('render', function(tree){
			var bt = new Ext.Button({
				tooltip: 'Add'
				,iconCls: 'icon-add-16x16'
				,handler: this.showWin
				,scope: this
			}, this);
			
			tree.getTopToolbar().add(bt);	
		}, this);	
	}
	,getForm: function(){
		if(this.form) return this.form;
		
		var o = this;
		var form = new Ext.FormPanel({
			frame: false
			,baseCls: 'x-plain'
			,monitorValid: true
			,labelWidth: 70
			,defaults: {anchor: '0', xtype: 'textfield', allowBlank: false}
			,items: [
				{
					name: 'name'
					,fieldLabel: 'Name'
				},{
					name: 'code'
					,fieldLabel: 'Code'
					,maskRe: /\w/
				},{
					name: 'default'
					,xtype: 'checkbox'
					,fieldLabel: 'Default'	
				}
			]
			,buttons: [
				{
					text: 'Save'
					,formBind: true
					,handler: function(){
						form.el.mask('Processing...');
						
						var values = form.getForm().getValues();
						API.language.add(values, function(r){
							form.el.unmask();
							
							o.tree.root.appendChild(new Ext.tree.TreeNode({
								id: r.id
								,text: values.name	
							}));	
						});
					}
				},{
					text: 'Reset'
					,handler: function(){form.getForm().reset();}	
				},{
					text: 'Close'
					,handler: function(){o.getWin().hide();}	
				}
			]	
		});
		
		this.form = form;
		
		return this.form;	
	}
	,getItems: function(){
		if(this.items) return this.items;
		this.items = [
			this.getForm()
		];
		return this.items;	
	}
	,getWin: function(){
		if(this.win) return this.win;
		
		var items = this.getItems();
		this.win = new App.SubWindow({
			title: 'Add Language'
			,renderTo: this.tree.ownerCt.body.id
			,plain: true
			,items: items
			,bodyStyle: 'padding:5px'
			//,layout: 'border'
			//,height: 300
			
		});
		return this.win;
	}
	,showWin: function(bt){
		this.getWin().show(bt.id);
	}
});